package com.hymjweb.demo.hymj.cash_set_manage.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.cash_set_manage.bean.BCashSetPojo;
import com.hymjweb.demo.hymj.master_bill_manage.load.mapper.BNativeBillLoadMapper;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BCashSetMapper {

    class BCashSetMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_cash_set t where 1 = 1 ");
            }}.toString();

            if (map != null) {
                String casherId = DataTypeUtil.toStringObject(map.get("casherId"));
                String queryDepartmentId = DataTypeUtil.toStringObject(map.get("queryDepartmentId"));

                if(casherId!=null){
                    sql += " and t.casher_id = " + DataTypeUtil.quoteStr(casherId);;
                }

                if(queryDepartmentId!=null){
                    sql += " and t.department_id = " + DataTypeUtil.quoteStr(queryDepartmentId);
                }
            }
            return  sql;
        }


        public String insert(BCashSetPojo pojo) {
            String sql = new SQL() {{
                INSERT_INTO("b_cash_set");
                VALUES("id","#{id}");
                VALUES("department_id","#{departmentId}");
                VALUES("casher_id","#{casherId}");
                VALUES("start_time","#{startTime}");
                VALUES("end_time","#{endTime}");
            }}.toString();
            return sql;
        }

        public String update(BCashSetPojo pojo) {
            String sql= new SQL() {{
                UPDATE("b_cash_set t");
                SET("t.department_id = #{departmentId}");
                SET("t.casher_id = #{casherId}");
                SET("t.start_time = #{startTime}");
                SET("t.end_time = #{endTime}");
                WHERE("t.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("b_cash_set");
                WHERE("id = " + id );
            }}.toString();
            return sql;
        }

        public String findObjectById(String id) {
            return new SQL() {{
                SELECT("t.*");
                FROM("b_cash_set t");
                WHERE("t.id = " + id);
            }}.toString();
        }

        public String queryCashSetByDeptIdAndTime(String departmentId, Date now){

            String nowTime = DateUtil.dateToStr_yyyy_MM_dd_HH_mm_ss(now);

            String sql = new SQL() {{
                SELECT("t.*");
                FROM("b_cash_set t");
                WHERE("t.department_id = " + DataTypeUtil.quoteStr(departmentId) + " and unix_timestamp(t.start_time)<= unix_timestamp(" + DataTypeUtil.quoteStr(nowTime) + ") and unix_timestamp(t.end_time)>unix_timestamp(" + DataTypeUtil.quoteStr(nowTime) + ")");
            }}.toString();

            return sql;
        }

        //同一个部门时间跨度存在重合，不允许设置
        public String findUK(String departmentId,long startTime,long endTime,String exceptId) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("b_cash_set t");
                WHERE("t.department_id = " + DataTypeUtil.quoteStr(departmentId) );
            }}.toString();
            sql += "and (";
            sql += "(";
            sql += " DATE_FORMAT(t.start_time,'%Y%m%d%k%i') <= " + startTime;
            sql += " and DATE_FORMAT(t.end_time,'%Y%m%d%k%i') >=  " + startTime;
            sql += ")";
            sql += "or ";
            sql += "(";
            sql += " DATE_FORMAT(t.start_time,'%Y%m%d%k%i') <= " + endTime;
            sql += " and DATE_FORMAT(t.end_time,'%Y%m%d%k%i') >=  " + endTime;
            sql += ")";

            sql += ")";

            if(!DataTypeUtil.isEmptyStr(exceptId)){
                sql += " and t.id != " + exceptId;
            }
            return sql;
        }


        public String queryCashierByCondition(String key){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("xt_user t");
                WHERE("t.id in (select distinct  t.casher_id from b_cash_set t ) and  (t.name like " + DataTypeUtil.quoteAllLikeStr(key) + " or t.mobile like " + DataTypeUtil.quoteAllLikeStr(key) + ")");
            }}.toString();
            return sql;
        }

    }
    @Results(id = "bCashSetMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "department_id", property = "departmentId"),
            @Result(column = "casher_id", property = "casherId"),
            @Result(column = "start_time", property = "startTime"),
            @Result(column = "end_time", property = "endTime"),
    })
    @SelectProvider(type = BCashSetMapperProvider.class, method = "findAll")
    List<BCashSetPojo> findAll(Map map);



    @ResultType(Map.class)
    @SelectProvider(type = BCashSetMapperProvider.class, method = "queryCashierByCondition")
    List<Map<String,Object>> queryCashierByCondition(String key);


    @ResultMap(value = "bCashSetMapId")
    @SelectProvider(type = BCashSetMapperProvider.class, method = "findObjectById")
    BCashSetPojo findObjectById(@Param("id") String id);

    @ResultMap(value = "bCashSetMapId")
    @SelectProvider(type = BCashSetMapperProvider.class, method = "queryCashSetByDeptIdAndTime")
    BCashSetPojo queryCashSetByDeptIdAndTime(String departmentId, Date now);

    @InsertProvider(type = BCashSetMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BCashSetPojo obj);

    @UpdateProvider(type = BCashSetMapperProvider.class, method = "update")
    int update(BCashSetPojo obj);

    @DeleteProvider(type = BCashSetMapperProvider.class, method = "delete")
    int delete(String id);

    @Results(id = "bCashSetFindUKMapId",value = {
            @Result(column = "id", property = "id"),
    })
    @SelectProvider(type = BCashSetMapperProvider.class, method = "findUK")
    BCashSetPojo findUK(String departmentId,long startTime,long endTime,String exceptId);

}
